package com.farben.system.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Optional;

import com.farben.common.utils.SecurityUtils;
import com.farben.common.utils.StringUtils;
import com.farben.common.utils.file.FileUtils;
import com.farben.common.utils.poi.ExcelUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.farben.system.mapper.AgingDesignConfigInfoMapper;
import com.farben.system.domain.AgingDesignConfigInfo;
import com.farben.system.service.IAgingDesignConfigInfoService;

/**
 * 适老化Service业务层处理
 *
 * @author farben
 * @date 2023-05-16
 */
@Service
public class AgingDesignConfigInfoServiceImpl implements IAgingDesignConfigInfoService {
    private static final Logger log = LoggerFactory.getLogger(AgingDesignConfigInfoServiceImpl.class);

    @Autowired
    private AgingDesignConfigInfoMapper agingDesignConfigInfoMapper;

    /**
     * 查询适老化
     *
     * @param id 适老化ID
     * @return 适老化
     */
    @Override
    public AgingDesignConfigInfo selectAgingDesignConfigInfoById(Long id) {
        return agingDesignConfigInfoMapper.selectAgingDesignConfigInfoById(id);
    }

    /**
     * 查询适老化列表
     *
     * @param agingDesignConfigInfo 适老化
     * @return 适老化
     */
    @Override
    public List<AgingDesignConfigInfo> selectAgingDesignConfigInfoList(AgingDesignConfigInfo agingDesignConfigInfo) {
        return agingDesignConfigInfoMapper.selectAgingDesignConfigInfoList(agingDesignConfigInfo);
    }

    /**
     * 新增适老化
     *
     * @param agingDesignConfigInfo 适老化
     * @return 结果
     */
    @Override
    public int insertAgingDesignConfigInfo(AgingDesignConfigInfo agingDesignConfigInfo) {
        String username = SecurityUtils.getUsername();
        agingDesignConfigInfo.setCreatedBy(username);
        agingDesignConfigInfo.setUpdatedBy(username);
        agingDesignConfigInfo.setCreatedDate(new Date());
        agingDesignConfigInfo.setUpdatedDate(new Date());
        return agingDesignConfigInfoMapper.insertAgingDesignConfigInfo(agingDesignConfigInfo);
    }

    /**
     * 修改适老化
     *
     * @param agingDesignConfigInfo 适老化
     * @return 结果
     */
    @Override
    public int updateAgingDesignConfigInfo(AgingDesignConfigInfo agingDesignConfigInfo) {
        return agingDesignConfigInfoMapper.updateAgingDesignConfigInfo(agingDesignConfigInfo);
    }


    /**
     * 批量删除适老化
     *
     * @param ids 需要删除的适老化ID
     * @return 结果
     */
    @Override
    public int deleteAgingDesignConfigInfoByIds(Long[] ids) {
        return agingDesignConfigInfoMapper.deleteAgingDesignConfigInfoByIds(ids);
    }

    /**
     * 删除适老化信息
     *
     * @param id 适老化ID
     * @return 结果
     */
    @Override
    public int deleteAgingDesignConfigInfoById(Long id) {
        return agingDesignConfigInfoMapper.deleteAgingDesignConfigInfoById(id);
    }

    @Override
    public boolean setPageConfig(AgingDesignConfigInfo info) {
        AgingDesignConfigInfo query = AgingDesignConfigInfo.builder().appId(info.getAppId()).fileName(info.getFileName()).build();
        AgingDesignConfigInfo result = agingDesignConfigInfoMapper.selectAgingDesignConfigInfoByAppIdAndFileName(query);
        if (result == null) {
            log.debug("setPageConfig:数据库没有查到文件记录");
            return false;
        }
        info.withIsHaveConfig("1").withConfirmFlag(info.getConfirm()).withFileContent(info.getText())
                .withAgingDesignType(StringUtils.isBlank(info.getAgingDesignType()) ? "0" : info.getAgingDesignType())
                .withUpdatedBy(SecurityUtils.getLoginUser().getUsername()).withUploadFlag(info.getConfirm()).withVersion(Optional.ofNullable(result.getVersion()).orElse(0L) + 1L);
        int ret = agingDesignConfigInfoMapper.updateAgingDesignConfigInfo(info);
        return !(ret == 0);
    }

    /**
     * 发布适老化
     *
     * @param agingDesignConfigInfo 适老化
     * @return 结果
     */
    @Override
    public int releaseAgingDesignConfigInfo(AgingDesignConfigInfo agingDesignConfigInfo) {
        uploadFile(agingDesignConfigInfo.getFileName(), agingDesignConfigInfo.getText(), agingDesignConfigInfo.getAgingDesignType());
        if ("1".equals(agingDesignConfigInfo.getConfirm())) {
            agingDesignConfigInfo.setPublishText(agingDesignConfigInfo.getText());
            agingDesignConfigInfo.setFileContent(null);
        } else {
            agingDesignConfigInfo.setFileContent(agingDesignConfigInfo.getText());
        }
        return agingDesignConfigInfoMapper.updateAgingDesignConfigInfo(agingDesignConfigInfo);
    }

    private void uploadFile(String fileName, String text, String agingDesignType) {
        String path = "1".equals(agingDesignType) ? "vision" : "voice-over";
        path = "D:\\farben\\" + path + "\\" + fileName;
        String replace = text.replace("&nbsp;", "");
        FileUtils.writeText2File(path, replace);
    }

    @Override
    public AgingDesignConfigInfo queryAppPageInfo(AgingDesignConfigInfo agingDesignConfigInfo) {
        return agingDesignConfigInfoMapper.selectAgingDesignConfigInfoByAppIdAndFileName(agingDesignConfigInfo);
    }
}
